package com.myProject.serves.Impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.myProject.bean.TExpert;
import com.myProject.bean.TMeet;
import com.myProject.dao.ExpertDao;
import com.myProject.dao.MeetDao;
import com.myProject.serves.ExpertSev;
import org.apache.ibatis.session.SqlSession;
import util.SqlSessionUtil;

import java.util.List;

public class ExpertSevImpl implements ExpertSev {
    @Override
    public List<TExpert> getAllExpert() {
        SqlSession sqlSession = SqlSessionUtil.getSqlSession();
        ExpertDao ed = sqlSession.getMapper(ExpertDao.class);
        List<TExpert> list = ed.getAllExpert();
        sqlSession.clearCache();
        return list;
    }

    @Override
    public List<TExpert> getExpertList(TExpert tExpert) {
        SqlSession sqlSession = SqlSessionUtil.getSqlSession();
        ExpertDao ed = sqlSession.getMapper(ExpertDao.class);
        List<TExpert> list = ed.getExpertList(tExpert);
        sqlSession.clearCache();
        return list;
    }

    @Override
    public PageInfo<TExpert> showExpertPage(int pageNo, int pageSize, TExpert tExpert) {
        SqlSession sqlSession = SqlSessionUtil.getSqlSession();
        ExpertDao ed = sqlSession.getMapper(ExpertDao.class);
        PageHelper.startPage(pageNo, pageSize);
        List<TExpert> list = ed.getExpertList(tExpert);
        PageInfo<TExpert> page = new PageInfo<TExpert>(list);
        SqlSessionUtil.commit();
//      sqlSession.close();
        return page;
    }

    @Override
    public boolean addExpert(TExpert tExpert) {
        SqlSession sqlSession = SqlSessionUtil.getSqlSession();
        ExpertDao ed = sqlSession.getMapper(ExpertDao.class);
        int x = ed.addExpert(tExpert);
        SqlSessionUtil.commit();
        sqlSession.close();
        return x > 0 ? true : false;
    }

    @Override
    public boolean deleteExpert(TExpert tExpert) {
        SqlSession sqlSession = SqlSessionUtil.getSqlSession();
        ExpertDao ed = sqlSession.getMapper(ExpertDao.class);
        int x = ed.deleteExpert(tExpert);
        SqlSessionUtil.commit();
        sqlSession.close();
        return x > 0 ? true : false;
    }

    @Override
    public boolean updateExpert(TExpert tExpert) {
        SqlSession sqlSession = SqlSessionUtil.getSqlSession();
        ExpertDao ed = sqlSession.getMapper(ExpertDao.class);
        int x = ed.updateExpert(tExpert);
        SqlSessionUtil.commit();
        sqlSession.close();
        return x > 0 ? true : false;
    }
}
